﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZkSurvey.Common.SurveyMgr;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using System.Data.Common;

namespace ZkSurvey.DAL.SurveyMgr
{
    public class SurveyDa
    {
        public static void InsertSurvey(Survey survey)
        {
            InsertSurvey(survey, DBHelper.GetDatabase(), null);
        }
        public static void InsertSurvey(Survey survey, TranscationHelper trans)
        {
            InsertSurvey(survey, trans.DataBase, trans.Transaction);
        }
        private static void InsertSurvey(Survey survey, Database db, DbTransaction trans)
        {
            object[] parameters = new object[]
            {
                survey.Title,
                survey.Description,
                survey.StartTime,
                survey.EndTime,
                survey.Creator,
                survey.CreateTime,
                survey.ValidationCode,
                survey.Code
            };

            try
            {
                if (trans == null)
                    survey.Id = Convert.ToInt32(db.ExecuteScalar("Usp_InsertSurvey", parameters));
                else
                    survey.Id = Convert.ToInt32(db.ExecuteScalar(trans, "Usp_InsertSurvey", parameters));
            }
            catch (System.Data.SqlClient.SqlException sex)      // 只捕获SqlException，其余抛出继续传播
            {
                DBHelper.ParseSqlException(sex, false);
            }
        }

        public static void DeleteSurvey(int id)
        {
            DeleteSurvey(id, DBHelper.GetDatabase(), null);
        }
        public static void DeleteSurvey(int id, TranscationHelper trans)
        {
            DeleteSurvey(id, trans.DataBase, trans.Transaction);
        }
        public static void DeleteSurvey(int id, Database db, DbTransaction trans)
        {
            try
            {
                if (trans == null)
                    db.ExecuteNonQuery("Usp_DeleteSurvey", id);
                else
                    db.ExecuteNonQuery(trans, "Usp_DeleteSurvey", id);
            }
            catch (System.Data.SqlClient.SqlException sex)      // 只捕获SqlException，其余抛出继续传播
            {
                DBHelper.ParseSqlException(sex, false);
            }
        }

        public static void UpdateSurvey(Survey survey)
        {
            UpdateSurvey(survey, DBHelper.GetDatabase(), null);
        }
        public static void UpdateSurvey(Survey survey, TranscationHelper trans)
        {
            UpdateSurvey(survey, trans.DataBase, trans.Transaction);
        }
        private static void UpdateSurvey(Survey survey, Database db, DbTransaction trans)
        {
            object[] parameters = new object[]
            {
                survey.Id,
                survey.Title,
                survey.Description,
                survey.StartTime,
                survey.EndTime,
                survey.Creator,
                survey.CreateTime,
                survey.ValidationCode,
                survey.Code
            };

            try
            {
                if (trans == null)
                    db.ExecuteNonQuery("Usp_UpdateSurvey", parameters);
                else
                    db.ExecuteNonQuery(trans, "Usp_UpdateSurvey", parameters);
            }
            catch (System.Data.SqlClient.SqlException sex)      // 只捕获SqlException，其余抛出继续传播
            {
                DBHelper.ParseSqlException(sex, false);
            }
        }

        public static Survey GetSurvey(int id)
        {
            Database db = DBHelper.GetDatabase();
            Survey survey = null;

            using (IDataReader dataReader = db.ExecuteReader("Usp_GetSurvey", id))
            {
                if (dataReader.Read())
                {
                    survey = PopulateSurvey(dataReader);
                }
            }
            return survey;
        }
        
        public static List<Survey> GetSurveyList()
        {
            Database db = DBHelper.GetDatabase();
            List<Survey> survies = new List<Survey>();

            using (IDataReader dataReader = db.ExecuteReader("Usp_GetSurveyList"))
            {
                while (dataReader.Read())
                {
                    survies.Add(PopulateSurvey(dataReader));
                }
            }

            return survies;
        }

        private static Survey PopulateSurvey(IDataReader dataReader)
        {
            Survey survey = new Survey();
            survey.Id = dataReader.GetInt32(0);
            survey.Title = dataReader.GetString(1);
            if (!dataReader.IsDBNull(2))
                survey.Description = dataReader.GetString(2);
            survey.StartTime = dataReader.GetDateTime(3);
            survey.EndTime = dataReader.GetDateTime(4);
            survey.Creator = dataReader.GetString(5);
            survey.CreateTime = dataReader.GetDateTime(6);
            survey.ValidationCode = dataReader.GetString(7);
            survey.Code = dataReader.GetGuid(8);

            return survey;
        }


    }
}
